generate "devise:install -q"
generate "devise:views"
generate "devise User"
generate "cancan:ability"

# Insert devise specific configuration
inject_into_file('config/environments/development.rb', :after => %[config.action_mailer.raise_delivery_errors = false]) do
  %q{
    
    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  }
end

inject_into_file('config/environments/production.rb', :after => %[# config.action_mailer.raise_delivery_errors = false]) do
  %q{
    
    # TODO set actual host of application
    # config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  }
end

inject_into_file('config/environments/test.rb', :after => %[config.action_mailer.delivery_method = :test]) do
  %q{
    
    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  }
end

## Devise routes
inject_into_file('config/routes.rb', :after => %[devise_for :users]) do
  %q{
    
    devise_scope :user do
      get "signup", :to => "devise/registrations#new"
      get "login", :to => "devise/sessions#new"
      get "logout", :to => "devise/sessions#destroy"
    end
  }
end

Dir.glob("app/views/devise/*/*.erb").each do |file|
  output_file = file.gsub('erb', 'haml')
  run "html2haml #{file} #{output_file}"
  remove_file file
end


## Login files
# signin = <<-SIGNIN
# <h2>Login</h2>
# 
# <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
#   <%= f.inputs do %>
#     <%= f.input :email, :as => :email %>
#     <%= f.input :password, :as => :password %>
#     <% if devise_mapping.rememberable? -%>
#       <%= f.input :remember_me, :as => :boolean %>
#     <% end -%>
#   <% end %>
#   <%= f.buttons do %>
#     <%= f.commit_button "Login" %>
#   <% end %>
# <% end %>
# 
# <%= render :partial => "devise/shared/links" %>
# SIGNIN
# 
# remove_file "app/views/devise/sessions/new.html.erb"
# create_file "app/views/devise/sessions/new.html.erb", signin

# signup = <<-SIGNUP
# <h2>Signup</h2>
# 
# <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
#   <%= devise_error_messages! %>
#   <%= f.inputs do %>
#     <%= f.input :email, :as => :email %>
#     <%= f.input :password, :as => :password %>
#   <% end %>
#   <%= f.buttons do %>
#     <%= f.commit_button "Signup" %>
#   <% end %>
# <% end %>
# 
# <%= render :partial => "devise/shared/links" %>
# SIGNUP
# 
# remove_file "app/views/devise/registrations/new.html.erb"
# create_file "app/views/devise/registrations/new.html.erb", signup

